import axios from 'axios'
import {Notify,Toast}  from "vant";
import router from "@/router";
export function request(config){
  const instance=axios.create({
    timeout:5000,
    baseURL:'https://api.shop.eduwork.cn'
  })
  instance.interceptors.request.use(config=>{
    const token=window.localStorage.getItem('token')
    if(token){
      config.headers.Authorization='Bearer'+token//授权认证
    }
   return config
  },
    err=>{
    }
    )
  function check(checkOption){
    let pas=''
    checkOption.forEach(item=>{
      pas+=item
    })
    Notify(pas)
  }
  instance.interceptors.response.use(res=>{
    return res.data?res.data:res
    },
    error => {
      if(error.response.status=='401'){//状态码为401的多是没有授权的
        Toast.fail('请先登录或登录失败')
        router.push({path:'/login'})
      }
    if(error.response.data.errors) {
      if (error.response.data.errors.name) {
        check(error.response.data.errors.name)
      }
      if (error.response.data.errors.password) {
        check(error.response.data.errors.password)
      }
      if (error.response.data.errors.email) {
        check(error.response.data.errors.email)
      }
    }
    })
return instance(config)
}
